package com.weai.portal.service.impl;

import com.weai.portal.entity.AiList;
import com.weai.portal.repository.AiListRepository;
import com.weai.portal.service.AiListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * AI清单服务实现类
 */
@Service
@Slf4j
public class AiListServiceImpl implements AiListService {

    @Autowired
    private AiListRepository aiListRepository;

    @Override
    public List<AiList> getAllAiLists() {
        log.debug("开始获取所有AI清单");
        try {
            List<AiList> aiLists = aiListRepository.findAll();
            log.info("成功获取所有AI清单，数量: {}", aiLists.size());
            return aiLists;
        } catch (Exception e) {
            log.error("获取所有AI清单失败", e);
            throw e;
        }
    }

    @Override
    public List<AiList> searchByTitle(String title) {
        log.debug("开始搜索AI清单，标题关键词: {}", title);
        try {
            List<AiList> aiLists = aiListRepository.searchByTitle(title);
            log.info("成功搜索AI清单，标题关键词: {}，数量: {}", title, aiLists.size());
            return aiLists;
        } catch (Exception e) {
            log.error("搜索AI清单失败，标题关键词: {}", title, e);
            throw e;
        }
    }

    @Override
    public AiList getAiListById(Long id) {
        log.debug("开始获取AI清单详情，ID: {}", id);
        try {
            AiList aiList = aiListRepository.findById(id)
                    .orElseThrow(() -> {
                        log.error("未找到AI清单，ID: {}", id);
                        return new RuntimeException("AiList not found with id: " + id);
                    });
            log.info("成功获取AI清单详情，ID: {}", id);
            return aiList;
        } catch (Exception e) {
            log.error("获取AI清单详情失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public AiList createAiList(AiList aiList) {
        log.debug("开始创建AI清单，标题: {}", aiList.getTitle());
        try {
            AiList savedAiList = aiListRepository.save(aiList);
            log.info("成功创建AI清单，ID: {}, 标题: {}", savedAiList.getId(), savedAiList.getTitle());
            return savedAiList;
        } catch (Exception e) {
            log.error("创建AI清单失败，标题: {}", aiList.getTitle(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public AiList updateAiList(Long id, AiList aiList) {
        log.debug("开始更新AI清单，ID: {}", id);
        try {
            AiList existingAiList = getAiListById(id);
            
            existingAiList.setTitle(aiList.getTitle());
            existingAiList.setDescript(aiList.getDescript());
            existingAiList.setUrlAddress(aiList.getUrlAddress());
            existingAiList.setUpdater(aiList.getUpdater());
            
            AiList updatedAiList = aiListRepository.save(existingAiList);
            log.info("成功更新AI清单，ID: {}", id);
            return updatedAiList;
        } catch (Exception e) {
            log.error("更新AI清单失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public void deleteAiList(Long id) {
        log.debug("开始删除AI清单，ID: {}", id);
        try {
            aiListRepository.deleteById(id);
            log.info("成功删除AI清单，ID: {}", id);
        } catch (Exception e) {
            log.error("删除AI清单失败，ID: {}", id, e);
            throw e;
        }
    }
} 